package com.ys.controller.api.finance;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwElectronicInvoice;
import com.ys.entity.hr.ThrOperateRegistration;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TcwElectronicInvoiceService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tcwElectronicInvoice")
public class ApiInterTcwElectronicInvoiceController {

	private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwElectronicInvoiceController.class);

	@Autowired
	private CommonApiService<TcwElectronicInvoice> commonApiService;

	@Autowired
	private TcwElectronicInvoiceService tcwElectronicInvoiceService;

	@RequiresPermissions("tcwElectronicInvoice:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TcwElectronicInvoice cond){
		cond.addLeftJoin("THR_OperateCompanyApply THRO ON THRO.vgsbm = A.vgsbmBuy ")
				.addField("THRO.vGsSh AS vGsShBuy,THRO.CUser AS cUser,THRO.vgsMc AS vgsMcBuy");//购买方公司
		cond.addLeftJoin("TB_CompanyInfo D ON D.vid = A.vgsbmSales ")
				.addField("D.tax AS vGsShSales,D.vname AS vnameSales");//销售公司
		cond = commonApiService.dataAuth(MenuId.TCW_ElectronicInvoice, cond);
		return commonApiService.searchLay(cond);
	}

	/**
	 * 修改
	 */
	@RequiresPermissions("tcwElectronicInvoice:view")
	@RequestMapping("/toEdit")
	@ResponseBody
	public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params){
		try {
			//修改
			return tcwElectronicInvoiceService.update(params);
		} catch (Exception e) {
			logger.error("修改出错：",e.getMessage(),e);
			return R.error(e.getMessage());
		}
	}


	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TcwElectronicInvoice cond, String conditions){
		try {
			cond.setPage(0).setSize(999999);
			cond.addLeftJoin("THR_OperateCompanyApply THRO ON THRO.vgsbm = A.vgsbmBuy ")
					.addField("THRO.vGsSh AS vGsShBuy,THRO.CUser AS cUser,THRO.vgsMc AS vgsMcBuy");//购买方公司
			cond.addLeftJoin("TB_CompanyInfo D ON D.vid = A.vgsbmSales ")
					.addField("D.tax AS vGsShSales,D.vname AS vnameSales");//销售公司
			//获取菜单字段数据
			List<YsCol> listField = new YsCol()
					.setMenuId(MenuId.TCW_ElectronicInvoice)
					.where("A.Code != 'ope'")
					.setFrontShow(1)
					.setOrderby("A.SortNo asc")
					.queryList();
			//获取数据
			List<Map<String, Object>> listData = cond.queryMap();
			//处理数据
			ExportExcelUtils<ThrOperateRegistration> util = new ExportExcelUtils<>(new ThrOperateRegistration());
			String toFilePath = util.exportExcel(listData, listField, "运营电子发票开票单");
			UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
			return R.ok(dto.getUrl());
		}catch (Exception e){
			logger.error("导出出错：",e.getMessage(),e);
			return R.error("导出失败，请稍后重试！");
		}
	}
}
